//学生上传视频时,按日期分类文件夹
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-')
  //return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


const formatTime2 = function (date, isyear) {
  /** 传递一个日期 返回这个日期的年份 或 月份
   * console.log('年份:',time.formatTime2(new Date,'year'))
    console.log('月份:',time.formatTime2(new Date,'month'))
    console.log('天:',time.formatTime2(new Date,'day'))
   * 第二个参数为 year返回年 month为月 day为日 
   * //userShow生成朋友圈图片时会用到  * 
   */
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  // const hour = date.getHours()
  // const minute = date.getMinutes()
  // const second = date.getSeconds()

  return isyear == 'year' ? year : (isyear == 'month' ? month : formatNumber(day))
  
}





const WeekDate = function () {
  //返回今天星期几,星期一返回1  星期六返回6 星期天返回0
  //userShow生成朋友圈图片时会用到 
  var now = new Date();
  var day = now.getDay();

  if (day == 0) {
    day = '日';
  } else if (day == 1) {
    day = '一';
  } else if (day == 2) {
    day = '二';
  } else if (day == 3) {
    day = '三';
  } else if (day == 4) {
    day = '四';
  } else if (day == 5) {
    day = '五';
  } else {
    day = '六'
  }


  return day;
}

const getDate = function (days) {
  //days传要加减的日期数，往前传正数，往后传负数,0返回当天时间   
  var Dates = new Date();
  Dates.setDate(Dates.getDate() + days);
  var mon = Dates.getMonth() + 1,
    day = Dates.getDate();
  if (mon < 10) {
    mon = "0" + mon; //月份小于10，在前面补充0
  }
  if (day < 10) {
    day = "0" + day; //日小于10，在前面补充0
  }
  return Dates.getFullYear() + "-" + mon + "-" + day
}



const daysDistance = function (date1, date2) {

  //求两个日期之间相隔天数的方法（开始时间,结束时间）
  //date1和date2是2019-06-18格式
  return Math.floor(Math.abs(Date.parse(date2) - Date.parse(date1)) / (24 * 3600 * 1000));
}

const suiji = function (n) {
  //获取一个指定长度的 随机码 add.js有调用
  var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_', '.'];
  var res = "";
  for (var i = 0; i < n; i++) {
    var id = Math.ceil(Math.random() * 38);
    res += chars[id];
  }
  return res;
}







module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  getWeekDate: WeekDate,
  getDate: getDate,
  daysDistance: daysDistance,
  suiji: suiji
}